<?php
/*******************************************************************************
 * @filename: products_controller.php
 *
 * @author: immer_baldos
 *
 * @description: controller for product model
 *
 * @history:
 *
 *   VERSION   DATE                   BY          NOTES
 *  ---------------------------------------------------------------
 *     1.0     September  9, 2009     immer       initial bake
 *
 *     2.0     September 12, 2009     immer       enable authetication
 *
 *     3.0     September 17, 2009     immer       removed admin routes
 *                                                add role based redirect logic
 *
 *     4.0     September 21, 2009     immer       implement search()
 *
 ******************************************************************************/
class ProductsController extends AppController {

	var $name = 'Products';
	var $helpers = array('Html', 'Form');
    var $components = array('Auth','Cookie', 'Session');

	function search() {
		if (!empty($this->data)) {
            $this->Session->setFlash(__('Search results below.', true));
			switch($this->data['Product']['select']){
			    case 0:$o = $this->Product->query('SELECT * FROM products WHERE products.id LIKE "%'.($this->data['Product']['search']).'%";');break;
			    case 1:$o = $this->Product->query('SELECT * FROM products WHERE products.name LIKE "%'.($this->data['Product']['search']).'%";');break;
			    case 2:$o = $this->Product->query('SELECT * FROM products WHERE products.description LIKE "%'.($this->data['Product']['search']).'%";');break;
               default:$o = $this->Product->query('SELECT * FROM products WHERE products.price = '.($this->data['Product']['search']).';');break;
			}
    	    $this->set('products', $o);
		} else {
		    $o = $this->Product->query('SELECT * FROM products LIMIT 0;');
    	    $this->set('products', $o);
        }
	    if ($this->Auth->user('role') != 'admin' && $this->Auth->user('role') != 'product officer' ){
            $this->redirect('/');
        }
	}

	function index() {
		$this->Product->recursive = 0;
		$this->set('products', $this->paginate());
	    if ($this->Auth->user('role') != 'admin' && $this->Auth->user('role') != 'product officer' ){
            $this->redirect('/');
        }
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Product.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('product', $this->Product->read(null, $id));
	    if ($this->Auth->user('role') != 'admin' && $this->Auth->user('role') != 'product officer' ){
            $this->redirect('/');
        }
	}

	function add() {
		if (!empty($this->data)) {
			$this->Product->create();
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(__('The Product has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Product could not be saved. Please, try again.', true));
			}
		}
	    if ($this->Auth->user('role') != 'admin' && $this->Auth->user('role') != 'product officer' ){
            $this->redirect('/');
        }
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Product', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Product->save($this->data)) {
				$this->Session->setFlash(__('The Product has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Product could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Product->read(null, $id);
		}
	    if ($this->Auth->user('role') != 'admin' && $this->Auth->user('role') != 'product officer' ){
            $this->redirect('/');
        }
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Product', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Product->del($id)) {
			$this->Session->setFlash(__('Product deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	    if ($this->Auth->user('role') != 'admin' && $this->Auth->user('role') != 'product officer' ){
            $this->redirect('/');
        }
	}



}
?>